<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>档案管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<style>
    .layui-form-pane .layui-form-label {
        width: auto !important;
    }

    .layui-table-cell {
        height: auto !important;
        white-space: normal;
    }
</style>
<!-- 表格状态列 -->
<script type="text/html" id="demoTreeTableState1">
    <input type="checkbox" id="ckStatus" lay-filter="ckStatus" value="{{d.id}}" lay-skin="switch"
           lay-text="归档|借阅" {{d.status==1?"checked":""}} readonly="true" disabled="disabled"/>
</script>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">查询方式</label>
                            <div class="layui-input-inline">
                                <select name="searchWay" lay-filter="searchSelect" lay-reqtext="请选择查询方式"
                                        lay-verify="required">
                                    <option value="id">按档案编号查询</option>
                                    <option value="chestid">按箱门编号查询</option>
                                    <option value="cabinetid">按柜子编号查询</option>
                                    <option value="chestid">按称派出所查询</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <input type="text" id="keyInput" lay-reqtext="请输入关键词哦" lay-verify="required" name="keyWord"
                                       placeholder="请选择查询方式" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn  layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"> 搜 索</i>
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <script type="text/html" id="toolbar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="add"> 添加档案</button>
            </div>
        </script>
        <fieldset class="table-search-fieldset">
            <legend>档案列表</legend>
            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        </fieldset>
        <script type="text/html" id="currentTableBar">
            <button class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">归档</button>
            {{# if(d.status === 0){ }}
            <button class="layui-btn layui-btn-xs layui-btn-disabled data-count-edit" lay-event="borrow1">借阅</button>
            {{# }else if(d.status === 1){ }}
            <button class="layui-btn layui-btn-xs layui-btn-warm data-count-edit" lay-event="borrow">借阅</button>
            {{# } }}
        </script>
    </div>
</div>
<script src="/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'layer'], function () {
        let $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer;

        table.render({
            elem: '#currentTableId',
            url: '/record/getRecord',
            toolbar: '#toolbar',
            width: 1250,
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            parseData:function(msg){
                console.log(msg);
                for (let i = 0; i < msg.data.length; i++) {
                    msg.data[i].updatetime=renderTime(msg.data[i].updatetime);
                }
                return msg;
            },
            cols: [
                [
                    {field: 'serial', width: 120, title: '档案编号'},
                    {field: 'name', width: 140, title: '档案名称'},
                    {field: 'chestname', width: 120, title: '箱子名称'},
                    {field: 'cabinetname', width: 120, title: '柜子名称'},
                    {field: 'address', width: 180, title: '档案地址'},
                    {field: 'updatetime', width: 260, title: '更新时间'},
                    {templet: '#demoTreeTableState1', width: 100, title: '状态'},
                    {title: '操作', minWidth: 80, toolbar: '#currentTableBar', fixed: "right", align: "center"}
                ]
            ],
            id: 'recordReload',
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true
        });

        function renderTime(date) {
            return new Date(+new Date(new Date(date).toJSON()) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
        }

        // 监听搜索操作:部门
        form.on('submit(data-search-btn)', function (data) {
            table.reload('recordReload', {
                url: '/record/reloadRecord'
                , page: {
                    curr: 1
                }
                , where: {
                    searchWay: data.field.searchWay,
                    keyWord: data.field.keyWord
                }
            }, 'data');
            return false;
        });
        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {
                //监听添加操作
                layer.open({
                    title: '添加档案',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['650px', '640px'],
                    content: '/page/filesManage/add',
                });
                $(window).on("resize", function () {
                    layer.close(layer.index);
                    window.parent.location.reload();
                });
            }
        });

        //监听指定开关
        form.on('switch(ckStatus)', function (obj) {
            console.log(obj.value);
            //传一个开关状态 obj.elem.checked
            //传一个type ,1:station 2:柜子 3:箱子
            /**
             * do ajax
             */
            //成功后刷新表格
            insTb.refresh();
            //根据开关id刷新
            let id = parseInt(obj.value);
            //刷新默认是全部不展开，所以在等刷新结束后再展开
            setTimeout(function () {
                insTb.expand(id);  // 展开指定节点
            }, 400);
        });

        //监听下拉选择框  搜索方式
        form.on('select(searchSelect)', function (data) {
            let way = parseInt(data.value);
            switch (way) {
                case 1:
                    $("#keyInput").prop("placeholder", "请输入档案编号");
                    break;
                case 2:
                    $("#keyInput").prop("placeholder", "请输入柜子编号");
                    break;
                case 3:
                    $("#keyInput").prop("placeholder", "请输入道路名称");
                    break;
                case 4:
                    $("#keyInput").prop("placeholder", "请输入箱门编号");
                    break;
                default:
                    $("#keyInput").prop("placeholder", "请选择查询方式");
            }

        });

        table.on('tool(currentTableFilter)', function (obj) {
            let data = obj.data;
            console.log(data);
            if (obj.event === 'edit') {
                layer.open({
                    title: '编辑档案',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['650px', '640px'],
                    content: '/page/filesManage/add?id=' + data.id//传值过去，通过档案id查值赋值修改
                });
                $(window).on("resize", function () {
                    layer.close(layer.index);
                    window.parent.location.reload();
                });
                return false;
            } else if (obj.event === 'borrow') {
                layer.confirm('确定借阅该档案', function () {
                    $.ajax({
                        url:'/record/recordBorrow',
                        type:`post`,
                        dataType:`json`,
                        data: data,
                        success:function (res) {
                            console.log(res);
                            if (res.code){
                                layer.msg('借阅成功');
                                //提交成功关闭窗口
                                layer.close(layer.index);
                                window.parent.location.reload();
                            }else{
                                layer.msg('借阅失败');
                            }
                        }
                    });
                    return false;
                });
            }
        });
    });
</script>
</body>
</html>